﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using NetFtp.Core.Utils;
using NetFtp.Core.DAO;

namespace NetFtp.Core.Login
{
    /// <summary>
    /// Un compte utilisateur de NetFtp.
    /// </summary>
    public class User
    {
        private int id;
        private string username;
        private string password;
        private IList<FtpAccount> ftpAccounts;
        private FtpAccountAccess accountsbdd;

        #region Constructeurs
        /// <summary>
        /// Constructeur l'utilisateur
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public User(string username, string password)
        {
            this.id = 0;
            this.username = username;
            this.password = StringUtils.HashString(password);

            ftpAccounts = new List<FtpAccount>();
            accountsbdd = new FtpAccountAccess();
        }
        /// <summary>
        /// Constructeur l'utilisateur
        /// </summary>
        public User(int id, string username, string password)
        {
            this.id = id;
            this.username = username;
            this.password = StringUtils.HashString(password);

            ftpAccounts = new List<FtpAccount>();
            accountsbdd = new FtpAccountAccess();
        }
        #endregion

        #region Proprietes
        /// <summary>
        /// Obtient l'id
        /// </summary>
        public int Id
        {
            get
            {
                return this.id;
            }
        }
        /// <summary>
        /// Obtient le username
        /// </summary>
        public string Username
        {
            get
            {
                return this.username;
            }

            set
            {
                if (string.IsNullOrEmpty(value))
                    throw new ArgumentNullException("username cannot be null");

                if (this.username != value)
                {
                    this.username = value;
                }
            }
        }
        /// <summary>
        /// défini le password
        /// </summary>
        public string Password
        {
            get
            {
                return this.password;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                    throw new ArgumentNullException("password cannot be null");

                if (this.password != value)
                {
                    this.password = StringUtils.HashString(value);
                }
            }
        }
        /// <summary>
        /// obtient/défini le password
        /// </summary>
        public IList<FtpAccount> FtpAccounts
        {
            get
            {
                return this.ftpAccounts;
            }
        }

        /// <summary>
        /// Permets de parcourir tout les comptes FTP de cet utilisateur.
        /// </summary>
        /// <returns></returns>
        public IEnumerator<FtpAccount> GetFtpAccountEnumerator()
        {
            return ftpAccounts.GetEnumerator();
        }

        public void AddFtpAccount(FtpAccount ftpAccount)
        {
            accountsbdd.DoSave(ftpAccount, this);
            ftpAccounts = accountsbdd.FtpAccounts;
        }

        public void RemoveFtpAccount(FtpAccount ftpAccount)
        {
            accountsbdd.DoRemove(ftpAccount);
            ftpAccounts = accountsbdd.FtpAccounts;
        }

        #endregion

        /// <summary>
        /// charge les comptes
        /// </summary>
        public void FillAccount()
        {
            accountsbdd.Fill(this);
            ftpAccounts = accountsbdd.FtpAccounts;
        }

        /// <summary>
        /// Retourne l'account ayant cet id
        /// </summary>
        public FtpAccount ReturnAccount(int id)
        {
            foreach (FtpAccount accounttmp in ftpAccounts)
            {
                if (accounttmp.Id == id)
                {
                    return accounttmp;
                }
            }
            return null;
        }
    }
}
